package edu.hawaii.its381b.client;

import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class CrabDisplay extends Composite{
	
	private int score=0;
	
	private Image scoreImage,
				  crabPortraitImage[] = new Image[6],
				  shellsBigImage[]    = new Image[6];
	
	private String crabInfo[] = {"",
								 "The Cone Snail Hermit Crab differs from most hermit crabs in that it lives in cone shells, not shells, with round openings.",
								 "The Left-Handed Hermit Crab is named for its very large left claw. When threatened, it pulls into it shell and seals the hole with its large left claw.",
								 "Like all hermit crabs, the Hidden Hermit Crab hides its soft abdomen (back end) in a snail shell to protect itself from predators.",
								 "The Elegant Hermit Crab (Calcinus elegans) is sometimes called the Halloween Hermit Crab due to its black and orange coloration.",
								 "The Jewelled Anemone Hermit Crab is named for its jewel-like eyes and for the protective anemones it carries on its shell."};
	
	private Label crabInfoText = new Label(""), currentScore= new Label(""); 
	VerticalPanel mainPanel;

	private HorizontalPanel scorePanel = new HorizontalPanel();
	private VerticalPanel crabPortraitPanel = new VerticalPanel();
	private VerticalPanel infoTextPanel = new VerticalPanel();
	private VerticalPanel shellImagePanel = new VerticalPanel();
	
	public CrabDisplay() {
		super();
		mainPanel = new VerticalPanel();
		mainPanel.setStyleName("displayInfoPanel");
		mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		initWidget(mainPanel);
		mainPanel.setSize("200px", "439px");

		mainPanel.add(scorePanel);
		mainPanel.add(crabPortraitPanel);
		mainPanel.setCellHorizontalAlignment(crabPortraitPanel, HasHorizontalAlignment.ALIGN_CENTER);
		mainPanel.add(infoTextPanel);
		mainPanel.add(shellImagePanel);
				
		mainPanel.setCellHorizontalAlignment(scorePanel, HasHorizontalAlignment.ALIGN_CENTER);
		scorePanel.setStyleName("displayInfoPanel");
		
		loadInfoImages();
		loadBigShellImages();
		
		SafeUri scoreURI = HermitCrabResources.INSTANCE.score().getSafeUri();
		scoreImage= new Image(scoreURI);
		
		scorePanel.add(scoreImage);
		scorePanel.setCellVerticalAlignment(scoreImage, HasVerticalAlignment.ALIGN_MIDDLE);
		scorePanel.add(currentScore);
		scorePanel.setCellHeight(currentScore, "50");
		scorePanel.setCellWidth(currentScore, "200");
		setScoreText();
		
		crabPortraitPanel.add(crabPortraitImage[1]);
		crabPortraitImage[1].setStyleName("displayInfoPanel");
		setInfoText(1);
		setScore(0);
		
		infoTextPanel.add(crabInfoText);
		setInfoTextCell();
		crabInfoText.setText(crabInfo[1]);
				
		shellsBigImage[1].setStyleName("displayInfoPanel");
		shellImagePanel.add(shellsBigImage[1]);
		mainPanel.setCellVerticalAlignment(shellImagePanel, HasVerticalAlignment.ALIGN_BOTTOM);
	}

	private void loadInfoImages(){
		SafeUri ConeInfoImageURI = HermitCrabResources.INSTANCE.crabInfoCone().getSafeUri();
		crabPortraitImage[1]= new Image(ConeInfoImageURI);
		
		SafeUri leftHandedInfoImageURI = HermitCrabResources.INSTANCE.crabInfoLeftHanded().getSafeUri();
		crabPortraitImage[2]= new Image(leftHandedInfoImageURI);
		
		SafeUri hiddenInfoImageURI = HermitCrabResources.INSTANCE.crabInfoHidden().getSafeUri();
		crabPortraitImage[3]= new Image(hiddenInfoImageURI);
		
		SafeUri elegantInfoImageURI = HermitCrabResources.INSTANCE.crabInfoElegant().getSafeUri();
		crabPortraitImage[4]= new Image(elegantInfoImageURI);
		
		SafeUri jewelledInfoImageURI = HermitCrabResources.INSTANCE.crabInfoJewelled().getSafeUri();
		crabPortraitImage[5]= new Image(jewelledInfoImageURI);
	}
	
	private void loadBigShellImages(){

		SafeUri shellsConeBigURI = HermitCrabResources.INSTANCE.shellsBigCone().getSafeUri();
		shellsBigImage[1]= new Image(shellsConeBigURI);
		
		SafeUri shellsLeftHandedBigURI = HermitCrabResources.INSTANCE.shellsBigLeftHanded().getSafeUri();
		shellsBigImage[2]= new Image(shellsLeftHandedBigURI);
		
		SafeUri shellsHiddenBigURI = HermitCrabResources.INSTANCE.shellsBigHidden().getSafeUri();
		shellsBigImage[3]= new Image(shellsHiddenBigURI);
		
		SafeUri shellsElegantBigURI = HermitCrabResources.INSTANCE.shellsBigElegant().getSafeUri();
		shellsBigImage[4]= new Image(shellsElegantBigURI);
		
		SafeUri shellsJewelledBigURI = HermitCrabResources.INSTANCE.shellsBigJewelled().getSafeUri();
		shellsBigImage[5]= new Image(shellsJewelledBigURI);
	}

	private void setInfoText(int currentCrab){
		crabInfoText.setDirectionEstimator(true);
		crabInfoText.setStyleName("crabDescription");
		crabInfoText.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		crabInfoText.setSize("185px", "");
	}
	
	private void setScoreText(){
		currentScore.setDirectionEstimator(true);
		currentScore.setStyleName("scoreDsplayPanel");
		currentScore.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		currentScore.setSize("100px", "");
	}
	
	private void setInfoTextCell(){
		mainPanel.setCellWidth(crabInfoText, "200");
		mainPanel.setCellVerticalAlignment(crabInfoText, HasVerticalAlignment.ALIGN_MIDDLE);
		mainPanel.setCellHorizontalAlignment(crabInfoText, HasHorizontalAlignment.ALIGN_CENTER);		
	}
	
	public void updateCrabInfoDisplayWindow(int currentCrab){
		//get rid of last information
		if(currentCrab == 1){//if 1, this is a reset, going from the fifth crab back to the first
			crabPortraitPanel.remove(crabPortraitImage[currentCrab+4]);
			infoTextPanel.remove(crabInfoText);
			shellImagePanel.remove(shellsBigImage[currentCrab+4]);
		}else{ //Normal increment to the next crab
		crabPortraitPanel.remove(crabPortraitImage[currentCrab-1]);
		infoTextPanel.remove(crabInfoText);
		shellImagePanel.remove(shellsBigImage[currentCrab-1]);
		}
		//Add information for the current crab
		crabPortraitPanel.add(crabPortraitImage[currentCrab]);
		infoTextPanel.add(crabInfoText);
		setInfoTextCell();
		crabInfoText.setText(crabInfo[currentCrab]);
		shellImagePanel.add(shellsBigImage[currentCrab]);
	}
	
	public void setScore(int newScore){
		String scoreString = new Integer(newScore).toString();
		currentScore.setText(scoreString);
		score = newScore;
	}
	
	public int getScore(){
		return score;
	}
	
	public void addToScore(int addToCurrentScore){
		score = score + addToCurrentScore*2; //times 2 so the top score approaches 10000 instead of 5000. Nicer feel.
		String scoreString = new Integer(score).toString();
		currentScore.setText(scoreString);
	}
}
